<?php

/**
 * book actions.
 *
 * @package    estate
 * @subpackage book
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class bookActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
    public function executeStep0(sfWebRequest $request)
    {
        if ( !$this->getUser()->getAttributeHolder()->hasNamespace( "booking" ) )
            $this->redirect( "@homepage" );
        
        $this->getContext()->getConfiguration()->loadHelpers( 'I18N', $this->getContext()->getModuleName());
        $this->step0 = new ApartmanBookingFormStep0();
        if ( $request->getMethod() == sfRequest::POST )
        {            
            $this->step0->bind( $this->getRequestParameter( 'booking_step0' ) );
            if ( $this->step0->isValid() )
            {
                foreach( $this->step0->getTaintedValues() as $key => $value )
                {
                    $this->getUser()->setAttribute( $key, $value, 'booking' );
                }
                
                $this->getUser()->setAttribute( 'step0', "success", "booking" );
                                
                $this->redirect( 'book/step1' );
            }
        }
        return sfView::SUCCESS;
    } 
 
    public function executeStep1(sfWebRequest $request)
    {
        if ( $request->hasParameter( 'apartman_id' ) )
        {
            $this->getUser()->setAttribute( 'apartman_id', $request->hasParameter( 'apartman_id' ), "booking" );
        }
        
        if ( $this->getUser()->getAttribute( 'step0', null, 'booking' ) != "success" )
        {    
            if ( !$this->getUser()->getAttributeHolder()->hasNamespace( "booking" ) )
                $this->redirect( "@homepage" );
            else
                $this->redirect( "book/step0" );
        }
            
        $this->getContext()->getConfiguration()->loadHelpers( 'I18N', $this->getContext()->getModuleName());
        $this->step1 = new ApartmanBookingFormStep1();
        if ( $request->getMethod() == sfRequest::POST )
        {
            $this->step1->bind( $this->getRequestParameter( 'booking_step1' ) );
            if ( $this->step1->isValid() )
            {
                foreach( $this->step1->getTaintedValues() as $key => $value )
                {
                    $this->getUser()->setAttribute( $key, $value, 'booking' );
                }
                $this->getUser()->setAttribute( 'step1', "success", "booking" );
                
                $this->redirect( 'book/step2' );
            }
        }
        return sfView::SUCCESS;
    }
  
    public function executeStep2(sfWebRequest $request )
    {       
        if ( $this->getUser()->getAttribute( 'step1', null, 'booking' ) != "success" )
        {    
            if ( !$this->getUser()->getAttributeHolder()->hasNamespace( "booking" ) )
                $this->redirect( "@homepage" );
            else
                $this->redirect( "book/step1" );
        }          
            
        $this->getContext()->getConfiguration()->loadHelpers( 'I18N', $this->getContext()->getModuleName());
        $this->step2 = new ApartmanBookingFormStep2();
        if ( $request->getMethod() == sfRequest::POST )
        {
            $this->step2->bind( $this->getRequestParameter( 'booking_step2' ) );
            if ( $this->step2->isValid() )
            {
                foreach( $this->step2->getTaintedValues() as $key => $value )
                {
                    $this->getUser()->setAttribute( $key, $value, 'booking' );
                }
                $this->getUser()->setAttribute( 'step2', "success", "booking" );                
                $this->redirect( 'book/step3' );
            }
        }
        return sfView::SUCCESS;     
    }
    
    public function executeStep3(sfWebRequest $request )
    {
        if ( $this->getUser()->getAttribute( 'step2', null, 'booking' ) != "success" )
        {    
            if ( !$this->getUser()->getAttributeHolder()->hasNamespace( "booking" ) )
                $this->redirect( "@homepage" );
            else
                $this->redirect( "book/step2" );
        }
            
        $this->getUser()->setAttribute( 'step3', "success", "booking" );            
            
    }
    
    public function executeBookingSuccess( sfWebRequest $request )
    {
        $connection = new Swift_Connection_SMTP( sfConfig::get( 'app_mail_account_smtp' ), Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_SSL );
        $connection->setUsername( sfConfig::get( 'app_mail_account_username' ) );
        $connection->setPassword( sfConfig::get( 'app_mail_account_password') );
         
        $mailer = new Swift( $connection );
        
        $message = new Swift_Message('Mail\'s subject', "mekkora!!", 'text/html');
        
        $mailer->send( $message, $recipients, sfConfig::get('app_mail_account_from' ) );
        $mailer->disconnect();        
    }
    
    public function executeAuthSuccess( sfWebRequest $request )
    {
        if ( $this->getUser()->getAttribute( 'step3', null, 'booking' ) != "success" )
        {    
            if ( !$this->getUser()->getAttributeHolder()->hasNamespace( "booking" ) )
                $this->redirect( "@homepage" );
            else
                $this->redirect( "book/step3" );
        }
    }
    
    public function executeOrderDonePP( sfWebrequest $request )
    {
        if ( $this->getUser()->getAttributeHolder()->hasNamespace( "booking" ) )
        {        
            $this->order = new ApartmanBooking();
            
            $this->updateFromSession();
            
            $this->order->save();
            
            $this->getUser()->getAttributeHolder()->removeNamespace( 'booking' );
            
            $this->paypalTransact = paypalTransactionPeer::retrieveByPK( $this->getRequestParameter( 'transact' ) );
            if ( $this->paypalTransact )
            {
                $this->paypalTransact->setForeignTable( "apartman_booking" );
                $this->paypalTransact->setForeignId( $this->order->getId() );
                $this->paypalTransact->save();
            }
            return sfView::SUCCESS;
        }
        else $this->redirect( "@homepage" );
    }
    
    public function updateFromSession()
    {
        $rawValues = $this->getUser()->getAttributeHolder()->getAll( 'booking' );
         
         if( isset( $rawValues['apartman_id'] ) )
             $this->order->setApartmanId( $rawValues['apartman_id'] );
         if( isset( $rawValues['date_arrive'] ) )
             $this->order->setCheckIn( mktime( 0, 0, 0, $rawValues['date_arrive']['month'], $rawValues['date_arrive']['day'], $rawValues['date_arrive']['year'] ) );
         if( isset( $rawValues['date_depart'] ) )
             $this->order->setCheckOut( mktime( 0, 0, 0, $rawValues['date_depart']['month'], $rawValues['date_depart']['day'], $rawValues['date_depart']['year'] ) );            
         if( isset( $rawValues['adults'] ) )
             $this->order->setPersons( $rawValues['adults'] );            
         if( isset( $rawValues['children'] ) )
             $this->order->setChildren( $rawValues['children'] );            
         if( isset( $rawValues['title'] ) )
             $this->order->setTitle( $rawValues['title'] );            
         if( isset( $rawValues['first_name'] ) )
             $this->order->setFirstName( $rawValues['first_name'] );            
         if( isset( $rawValues['last_name'] ) )
             $this->order->setLastName( $rawValues['last_name'] );            
         if( isset( $rawValues['birth_name'] ) )
             $this->order->setBirthName( $rawValues['birth_name'] );            
         if( isset( $rawValues['address'] ) )
             $this->order->setAddress( $rawValues['address'] );            
         if( isset( $rawValues['city'] ) )
             $this->order->setCity( $rawValues['city'] );            
         if( isset( $rawValues['state'] ) )
             $this->order->setState( $rawValues['state'] );            
         if( isset( $rawValues['country'] ) )
             $this->order->setCountry( $rawValues['country'] );            
         if( isset( $rawValues['email'] ) )
             $this->order->setEmail( $rawValues['email'] );            
         if( isset( $rawValues['phone'] ) )
             $this->order->setPhone( $rawValues['phone'] );            
         if( isset( $rawValues['fax'] ) )
             $this->order->setFax( $rawValues['fax'] );
         if( isset( $rawValues['zip'] ) )
             $this->order->setZip( $rawValues['zip'] );                     
         if( isset( $rawValues['cellphone'] ) )
             $this->order->setCellphone( $rawValues['cellphone'] );            
         if( isset( $rawValues['arrive_by'] ) )
             $this->order->setArriveBy( $rawValues['arrive_by'] );            
         if( isset( $rawValues['arrive_details'] ) )
             $this->order->setArriveDetails( $rawValues['arrive_details'] );            
         if( isset( $rawValues['comments'] ) )
             $this->order->setMemo( $rawValues['comments'] );
         if( isset( $rawValues['require_transfer'] ) )
             $this->order->setRequireTransfer( $rawValues['require_transfer'] );                    
    }
    
    public function executeTransfer()
    {
        
    }
    
    public function executeOrderDoneMT( sfWebrequest $request )
    {
        if ( $this->getUser()->getAttributeHolder()->hasNamespace( "booking" ) )
        {        
            $this->order = new ApartmanBooking();
            
            $this->updateFromSession();
            
            $this->order->save();
            
            $this->getUser()->getAttributeHolder()->removeNamespace( 'booking' );
            
            return sfView::SUCCESS;
        }
        else $this->redirect( "@homepage" );
    }
        
}
